//
//  SignViewController.m
//  FastMedical
//
//  Created by 李通 on 16/7/6.
//  Copyright © 2016年 chivalrous. All rights reserved.
//

#import "SignViewController.h"

#import "LeftViewController.h"

#import "Use.h"


#define kFilePath(fileName) ([NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:fileName])

@interface SignViewController ()

@property (nonatomic, strong) UIImageView * logoView;
@property (nonatomic, strong) UIImageView * logoLabel;
@property (nonatomic, strong) UITextField * accountTF;
@property (nonatomic, strong) UITextField * passwordTF;
@property (nonatomic, strong) UIButton    * loginButton;

@property (nonatomic,strong) NSDictionary *personInfo;

@end

@implementation SignViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}


-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    
    [self.view endEditing:YES];
}

- (void)setupUI{
    
    self.title = @"登录";
    
    /*!
     *  添加
     */
    [self.view addSubview:self.logoView];
    [self.view addSubview:self.logoLabel];
    [self.view addSubview:self.accountTF];
    [self.view addSubview:self.passwordTF];
    [self.view addSubview:self.loginButton];
    //对用户名解档
    

    //    NSDictionary *dict = [NSKeyedUnarchiver unarchiveObjectWithFile:kFilePath(@"user.data")];
    NSString *nameStr = [[NSUserDefaults standardUserDefaults]objectForKey:@"phoneNumber"];
    if (nameStr) {
        self.accountTF.text = nameStr;
        [self.passwordTF becomeFirstResponder];
    }

    
    /*!
     *  设置布局
     */
    
    CGFloat padding = (SCREEN_WIDTH==320) ? 10 : 70;
    
    [self.logoView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(64+padding);
        make.centerX.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(70, 90));
    }];
    
    [self.logoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.logoView.mas_bottom).offset(2);
        make.centerX.equalTo(self.logoView);
        make.size.mas_equalTo(CGSizeMake(70, 45));
    }];
    
    [self.accountTF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.logoLabel.mas_bottom).offset(20);
        make.height.mas_equalTo(35);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
    }];
    
    [self.passwordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.accountTF.mas_bottom).offset(20);
        make.height.mas_equalTo(35);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
    }];
    
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.passwordTF.mas_bottom).offset(30);
        make.height.mas_equalTo(45);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
    }];
}


#pragma mark  - 逻辑功能实现区域
//**************************登录功能实现*******************************
//点击屏幕回退键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.view endEditing:YES];
    [SVProgressHUD dismiss];
}
//点击登录
- (void)didClickSign {
    
    [SVProgressHUD show];
    
    NSString *username = self.accountTF.text;
    NSString *password = self.passwordTF.text;
    
    if ([username isEqualToString:@"12"] && [password isEqualToString:@"12"]) {
        
        [[NSUserDefaults standardUserDefaults]setObject:@{@"username":username,@"password":password} forKey:@"account"];
        
        NSDictionary *paramDic = @{@"login_token":@"985c96ce43bb5ef69de3e1ad8afaa83f"};
        
        [[NetWorkTool sharedWorkTool]POST:@"http://iosapi.itcast.cn/carelinkQuickLogin.json.php"  parameters:paramDic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            if (responseObject[@"data"] == nil) {
                return ;
            }
            self.personInfo = responseObject[@"data"];
            [NSKeyedArchiver archiveRootObject:self.personInfo toFile:kFilePath(@"user.data")];
            [[NSNotificationCenter defaultCenter]postNotificationName:@"upload" object:nil];
            [SVProgressHUD dismiss];
            [self.navigationController popToRootViewControllerAnimated:YES];
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            [SVProgressHUD showErrorWithStatus:@"网络错误"];
        }];
        
    } else {
        
        [SVProgressHUD showErrorWithStatus:@"用户名或密码错误请重新输入"];
        self.passwordTF.text = @"";
    }
}

//**************************登录功能实现*******************************

#pragma mark  - 数据懒加载区域
- (UIImageView *)logoView{
    
    if (_logoView == nil) {
        _logoView = [[UIImageView alloc] init];
        _logoView.image = [UIImage imageNamed:@"product_img_logo_nor.png"];
    }
    return _logoView;
}

- (UIImageView *)logoLabel{
    
    if (_logoLabel == nil) {
        _logoLabel = [[UIImageView alloc] init];
        _logoLabel.image = [UIImage imageNamed:@"product_text_logo_nor.png"];
    }
    return _logoLabel;
}


- (UITextField *)accountTF{
    if (_accountTF == nil) {
        _accountTF = [[UITextField alloc] init];
        _accountTF.placeholder = @"请输入用户名";
        _accountTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        _accountTF.borderStyle = UITextBorderStyleRoundedRect;
        
    }
    return _accountTF;
    
}

- (UITextField *)passwordTF{
    if (_passwordTF == nil) {
        _passwordTF = [[UITextField alloc] init];
        _passwordTF.placeholder = @"请输入密码";
        _passwordTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        _passwordTF.borderStyle = UITextBorderStyleRoundedRect;
        //增加密码密文输入保障用户安全
        _passwordTF.secureTextEntry = YES;
    }
    return _passwordTF;
}

-(UIButton *)loginButton{
    
    if (_loginButton == nil) {
        _loginButton = [[UIButton alloc] init];
        [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
        _loginButton.backgroundColor = kThemeColor;
        _loginButton.titleLabel.font = FONT(20);
        [_loginButton addTarget:self
                         action:@selector(didClickSign)
               forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginButton;
}

//存储网络数据
- (NSDictionary *)personInfo {
    
    if (!_personInfo) {
        
        _personInfo = [NSDictionary dictionary];
    }
    return _personInfo;
}

@end
